/**
 * @Title: crawler_JD.java
 * @Package model
 * @Description: 京东爬虫处理类
 * @author ffftzh
 * @date 2015年12月13日 下午9:05:49
 * @version V1.0
 */
package edu.cn.nlsde.tmfst.crawler.processor;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import edu.cn.nlsde.Utils.Utils;
import edu.cn.nlsde.tmfst.model.Comment;
import edu.cn.nlsde.tmfst.model.Photo;
import edu.cn.nlsde.tmfst.model.Product;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;

import java.util.ArrayList;

/**
 * @author ffftzh
 * @ClassName: crawler_JD_m
 * @Description: 京东爬虫处理类
 * @date 2015年12月13日 下午9:05:49
 */
public class crawler_TM implements PageProcessor {
    static Logger mylog = LoggerFactory.getLogger(crawler_TM.class.getName());
    public String product_id = "";
    public String seller_id = "";
    public boolean status = false;
    long before = System.currentTimeMillis();

    private Site site = Site.me()
            .setSleepTime(10)
            .addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8;charset=utf-8")
            .setUserAgent("Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36");

    private void process_product_page(Page page) {
        try {
            this.product_id = page.getHtml().regex("itemId:\\\"(\\d+)\\\",sellerId:\\\"(\\d+)\\\"", 1).toString();
            this.seller_id = page.getHtml().regex("itemId:\\\"(\\d+)\\\",sellerId:\\\"(\\d+)\\\"", 2).toString();

//            double rate = 0;
//            try {
//                rate = Double.parseDouble(page.getHtml().regex("<em class=\\\"count\\\" title=\\\"[^<>\\\"]\\\">([0-9\\.])</em>", 1).toString());
//            } catch (Exception ex) {
//            }
            Product pd = new Product();
            pd.setPlatform(1);
//            pd.setProduct_rate((int) Math.round(rate / 5 * 100));
            pd.setProduct_id("Tmall_" + this.product_id);
            pd.save_model();
            if (this.product_id != null || this.seller_id != null) {
                status = true;
                page.addTargetRequest("https://rate.tmall.com/list_detail_rate.htm?itemId=" + this.product_id + "&sellerId=" + this.seller_id + "&order=3&append=0&content=1&tagId=&posi=&picture=&&currentPage=1&pageSize=30");
            }
        } catch (Exception e) {
            mylog.error("", e);

        }
    }

    @Override
    public void process(Page page) {
        if (status == false) {
            process_product_page(page);
        } else {
            try {
                Thread.sleep(150);
                long curren = System.currentTimeMillis();
                System.out.println("##########################" + (curren - before) + "#####################");
                before = curren;
                JSONObject rawObject = JSONObject.parseObject("{" + page.getRawText() + "}").getJSONObject("rateDetail");
                JSONArray result = rawObject.getJSONArray("rateList");

                for (Object item : result) {
                    JSONObject oneComment = (JSONObject) item;

                    if (oneComment.getString("rateContent").length() <= 4) {
                        continue;
                    }

//                    System.out.println(oneComment.getString("rateDate"));
                    Comment one = new Comment();
                    one.comment_id = "Tmall_" + oneComment.getString("id");
                    one.comment_content = oneComment.getString("rateContent");

                    one.comment_time = Utils.String2Date(oneComment.getString("rateDate"), "yyyy-MM-dd HH:mm:ss");
                    one.product_id = "Tmall_" + this.product_id;
                    one.save_model();
                    if (oneComment.getString("pics").trim().length() > 10) {
                        JSONArray photos = oneComment.getJSONArray("pics");
                        if (photos != null && photos.size() > 0) {
                            for (Object item2 : photos) {
                                Photo po = new Photo();
                                po.comment_id = one.comment_id;
                                po.photo_url = (String) item2;
                                po.save_model();
                            }

                        }
                    }
                }

                int lastpage = rawObject.getJSONObject("paginator").getInteger("lastPage");
                lastpage = lastpage > 30 ? 30 : lastpage;
                int nowpage = rawObject.getJSONObject("paginator").getInteger("page");
                if (nowpage == 1) {
                    ArrayList<String> urls = new ArrayList<>();
                    for (int i = 1; i < lastpage; i++) {
                        urls.add(page.getUrl().replace("currentPage=\\d+", "currentPage=" + (i + 1)).toString());
                    }
                    page.addTargetRequests(urls);
                }
                curren = System.currentTimeMillis();
                System.out.println("$$$$$$$$$$$$$$$$$$$$$" + (curren - before) + "$$$$$$$$$$$$$$$$$$$$$");
            } catch (Exception e) {
                mylog.error("error text:" + page.getRawText() + "##########", e);

            }
        }


    }

    @Override
    public Site getSite() {
        return site;
    }

}
